import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req, SetMetadata } from "@nestjs/common";
import { RoleService } from "./role.service";
import { CreateRoleDto } from "./dto/create-role.dto";
import { UpdateRoleDto } from "./dto/update-role.dto";
import { FastGuard } from "src/fast/fast.guard";
import { Request } from 'express';
import { AuthGuard } from "src/auth/auth.guard";
import { RolesGuard } from "src/auth/roles.guard";

@Controller("/mg/role")

export class RoleController {
    constructor(private readonly roleService: RoleService) { }

    @UseGuards(AuthGuard, RolesGuard) 
    @SetMetadata("roles", ["超级管理员"])
    @Post()
    create(@Body() createRoleDto: CreateRoleDto) {
        return this.roleService.create(createRoleDto);
    }

    // @UseGuards(FastGuard)
    @Get()
    findAll(@Req() request: Request) {
        console.log("####", (request as any).conn);
        console.log(request.ip, `${process.env.jwt_secret}`)
        const userAgent = request.headers['user-agent']; // 获取User-Agent
        console.log(userAgent);
        return this.roleService.findAll();
    }

    @Get(":id")
    findOne(@Param("id") id: string) {
        return this.roleService.findOne(id);
    }

    @UseGuards(AuthGuard, RolesGuard) 
    @SetMetadata("roles", ["超级管理员"])
    @Patch(":id")
    update(@Param("id") id: string, @Body() updateRoleDto: UpdateRoleDto) {
        return this.roleService.update(id, updateRoleDto);
    }

    @UseGuards(AuthGuard, RolesGuard) 
    @SetMetadata("roles", ["超级管理员"])
    @Delete(":id")
    remove(@Param("id") id: string) {
        return this.roleService.remove(id);
    }
}
